<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Dns extends CI_Controller {

    var $hostsheadfile = '/home/servidor/dns/egsf/egsf.tk.hosts.head';
    var $hostsfile = '/home/servidor/dns/egsf/egsf.tk.hosts';

    public function Dns() {
        parent::__construct();

//        $this->load->model('user_model', 'user');
        $this->load->model('host_model', "host");
        $this->load->model('zone_model', "zone");
    }

    public function index() {
        
    }

    public function filegen() {
        if (isset($_SERVER['USERNAME']) && !$_SERVER['REMOTE_ADDR'] && $_SERVER['USERNAME'] == 'root') {

            $hosts = $this->host->listar();

            if ($fp = fopen($this->hostsheadfile, 'r')) {
                $ret = fread($fp, filesize($this->hostsheadfile));
                fclose($fp);
            }

            echo $ret;

            $hostsfile = $ret;

            foreach ($hosts as $key => $host) {
                $hostsfile.= $host->nome . "." . $host->zone()->name . "." . "\tIN\tA\t" . $host->ip . PHP_EOL;
            }

            echo $hostsfile;

            if ($fp = fopen($this->hostsfile, 'w+')) {
                fwrite($fp, $hostsfile);
                fclose($fp);
            }
        } else {
            echo "denied!";
        }
    }

}
